<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="devName">
      <el-input v-model="dataForm.devName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="sceneId">
      <el-input v-model="dataForm.sceneId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="sceneName">
      <el-input v-model="dataForm.sceneName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="brandId">
      <el-input v-model="dataForm.brandId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="brandName">
      <el-input v-model="dataForm.brandName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="consumerId">
      <el-input v-model="dataForm.consumerId" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          devId: 0,
          devName: '',
          sceneId: '',
          sceneName: '',
          brandId: '',
          brandName: '',
          consumerId: ''
        },
        dataRule: {
          devName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sceneId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sceneName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          brandId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          brandName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          consumerId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.devId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.devId) {
            this.$http({
              url: this.$http.adornUrl(`/product/devsecbracon/info/${this.dataForm.devId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.devName = data.devSecBraCon.devName
                this.dataForm.sceneId = data.devSecBraCon.sceneId
                this.dataForm.sceneName = data.devSecBraCon.sceneName
                this.dataForm.brandId = data.devSecBraCon.brandId
                this.dataForm.brandName = data.devSecBraCon.brandName
                this.dataForm.consumerId = data.devSecBraCon.consumerId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/devsecbracon/${!this.dataForm.devId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'devId': this.dataForm.devId || undefined,
                'devName': this.dataForm.devName,
                'sceneId': this.dataForm.sceneId,
                'sceneName': this.dataForm.sceneName,
                'brandId': this.dataForm.brandId,
                'brandName': this.dataForm.brandName,
                'consumerId': this.dataForm.consumerId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
